/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef OHOS_ABILITY_RUNTIME_ABILITY_START_WITH_WAIT_OBSERVER_MANAGER
#define OHOS_ABILITY_RUNTIME_ABILITY_START_WITH_WAIT_OBSERVER_MANAGER

#include <map>
#include <mutex>

#include "ability_record.h"
#include "ability_start_with_wait_observer_stub.h"
#include "ability_start_with_wait_observer_utils.h"
#include "nocopyable.h"
#include "want.h"

namespace OHOS {
namespace AAFwk {
using TerminateReason = AbilityStartWithWaitObserverUtil::TerminateReason;
struct AbilityForegroundInfo {
    bool coldStart = false;
    int32_t observerId = 0;
    sptr<AbilityStartWithWaitObserverRecipient> deathRecipient = nullptr;
    int64_t startTime = 0;
    int64_t foregroundTime = 0; // ability foreground time
    std::string bundleName;
    std::string abilityName;
};

class AbilityStartWithWaitObserverManager {
public:
    static AbilityStartWithWaitObserverManager& GetInstance();
    int32_t RegisterObserver(Want &want, sptr<IAbilityStartWithWaitObserver> observer);
    void UnregisterObserver(sptr<IAbilityStartWithWaitObserver> observer);
    void NotifyAATerminateWait(Want& want, TerminateReason reason = TerminateReason::TERMINATE_FOR_NONE);
    void NotifyAATerminateWait(std::shared_ptr<AbilityRecord> abilityRecord,
        TerminateReason reason = TerminateReason::TERMINATE_FOR_NONE);
    void SetColdStartForShellCall(std::shared_ptr<AbilityRecord> abilityRecord);

private:
    AbilityStartWithWaitObserverManager() = default;
    ~AbilityStartWithWaitObserverManager() = default;

    sptr<AbilityStartWithWaitObserverRecipient> GenerateDeathRecipient(sptr<IAbilityStartWithWaitObserver> observer);

    std::mutex oberverMutex_;
    int32_t observerId_ = 0;
    std::map<sptr<IAbilityStartWithWaitObserver>, AbilityForegroundInfo> observerList_;
    DISALLOW_COPY_AND_MOVE(AbilityStartWithWaitObserverManager);
};
} // namespace AAFwk
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_ABILITY_START_WITH_WAIT_OBSERVER_MANAGER